package com.stlm2.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * Author：fuchunyu
 * Date：2017/8/7
 * Description：极光推送
 */
public class PushUtil {


    private static String  AppKey="112555aaaa4cb0ef389c42d1";
    private static String MasterSecret="1b083724dad61244f87d34c2";
    private static Boolean Test =true;//True 表示推送生产环境，False 表示要推送开发环境   默认是开发


    private static String  YongChengAppKey="ca784f7ce12dc7ff863afb6f";
    private static String YongChengMasterSecret="63a811dc7d166b9ae7778863";

    private static String  XtdAppKey="4f60cbfcb7caa6d1481617b3";
    private static String XtdMasterSecret="2131f08e0c75f8fdbe1cb121";


    private static JPushClient switchoverApp(String companyCode){
        JPushClient jpushClient = new JPushClient(MasterSecret, AppKey);
        if(companyCode.equals("2")){
            jpushClient = new JPushClient(YongChengMasterSecret, YongChengAppKey);
        }
        if(companyCode.equals("3")){
            jpushClient = new JPushClient(XtdMasterSecret, XtdAppKey);
        }
        return jpushClient;

    }
    /**
     * 给所有平台的所有用户发通知
     */
    public static void sendAllsetNotification(String message, Map<String, String> extras,String companyCode)
    {
        JPushClient jpushClient = switchoverApp(companyCode);
        PushPayload payload = buildPushObject_all_alias_alert(message, extras);
        try
        {
            PushResult result = jpushClient.sendPush(payload);
        }
        catch (APIConnectionException e)
        {
            System.out.println(e);
        }
        catch (APIRequestException e)
        {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. " + e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }
    /**
     * 给所有平台的所有用户发消息
     */
    public static void sendAllMessage(String message,Map<String, String> extras,String companyCode)
    {
        JPushClient jpushClient = switchoverApp(companyCode);
        PushPayload payload = buildPushObject_all_alias_Message(message, extras);
        try
        {
            PushResult result = jpushClient.sendPush(payload);
        }
        catch (APIConnectionException e)
        {
            System.out.println(e);
        }
        catch (APIRequestException e)
        {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. " + e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }
    /**
     * 发送通知
     */
    private static PushPayload buildPushObject_all_alias_alert(String message,
                                                               Map<String, String> extras)
    {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                // 设置平台
                .setAudience(Audience.all())
                // 按什么发送 tag alia
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(message)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                // 发送消息
                .setOptions(Options.newBuilder().setApnsProduction(Test).build()).build();
        //设置ios平台环境  True 表示推送生产环境，False 表示要推送开发环境   默认是开发
    }
    /**
     * 发送透传消息
     */
    private static PushPayload buildPushObject_all_alias_Message(String message,
                                                                 Map<String, String> extras)
    {
        return PushPayload.newBuilder().setPlatform(Platform.all())
                // 设置平台
                .setAudience(Audience.all())
                // 按什么发送 tag alia
                .setMessage(Message.newBuilder().setMsgContent(message).addExtras(extras).build())
                // 发送通知
                .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
        //设置ios平台环境  True 表示推送生产环境，False 表示要推送开发环境   默认是开发
    }

    /**
     * 客户端 给所有平台的一个或者一组用户发送信息
     */
    public static void sendAlias(String message, List<String> aliasList,Map<String, String> extras,String companyCode)
    {
        JPushClient jpushClient = switchoverApp(companyCode);

        PushPayload payload = allPlatformAndAlias(message, extras, aliasList);
        try
        {
            PushResult result = jpushClient.sendPush(payload);
        }
        catch (APIConnectionException e)
        {
            System.out.println(e);
        }
        catch (APIRequestException e)
        {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. " + e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }

    /**
     * 极光推送：生成向一个或者一组用户发送的消息。
     */
    private static PushPayload allPlatformAndAlias(String alert, Map<String, String> extras,
                                                   List<String> aliasList)
    {

        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliasList))
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(alert)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                .setOptions(Options.newBuilder().setApnsProduction(Test).build()).build();
    }
    /**
     * Author：fuchunyu
     * Date：2017/8/15
     * Description：给单个用户推送
     */

    private static PushPayload allPlatformAndAlias(String alert, Map<String, String> extras,
                                                   String alias)
    {

        return PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(alert)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                .setOptions(Options.newBuilder().setApnsProduction(Test).build()).build();
    }
    /**
     * 客户端 给平台的一个或者一组标签发送消息。
     */
    public static void sendTag(String message, List<String> tagsList,Map<String, String> extras,String companyCode)
    {
        JPushClient jpushClient =switchoverApp(companyCode);
        PushPayload payload = allPlatformAndTag(message, extras, tagsList);
        try
        {
            PushResult result = jpushClient.sendPush(payload);
        }
        catch (APIConnectionException e)
        {
            System.out.println(e);
        }
        catch (APIRequestException e)
        {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. " + e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }

    /**
     * 极光推送：生成向一组标签进行推送的消息。
     */
    private static PushPayload allPlatformAndTag(String alert, Map<String, String> extras,
                                                 List<String> tagsList)
    {

        return PushPayload
                .newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag(tagsList))
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(alert)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                .setOptions(Options.newBuilder().setApnsProduction(Test).build()).build();
    }

    /**
     * Author：fuchunyu
     * Date：2017/8/15
     * Description：给单个用户推送通知
     */

    public static void sendAlias(String message, String alias,Map<String, String> extras,String companyCode)
    {
        JPushClient jpushClient = switchoverApp(companyCode);

        PushPayload payload = allPlatformAndAlias(message, extras, alias);
        try
        {
            PushResult result = jpushClient.sendPush(payload);
//            System.out.println(result);
        }
        catch (APIConnectionException e)
        {
            System.out.println(e);
        }
        catch (APIRequestException e)
        {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. " + e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }

/*    public static void main(String[] args)
    {
        List<String> sendAlias = new ArrayList<String>();
        sendAlias.add("15528196521");
        sendAlias.add("15738961968");

        Map<String,String> extras=new HashMap<String, String>();
        extras.put("type","xinwen");
        extras.put("url","http://www.baidu.com");
        sendAlias("淅方法","15528196521",extras);

    }*/


}
